import { defineStore } from "pinia";
import { ref, computed } from "vue";

const useCartStore = defineStore('cart', {
    state: () => {
        const cartItem = {
            id: '',
            title: '',
            price: 0,
            image: '',
            count: 1,
            check: true
        }
        const cart = ref([])
        const totalAmount = ref(0)
        const checkAll=computed(()=>{
            if(cart.value.every(item=>item.check==true))return Boolean(true)
            else return Boolean(false)
            
        })

        const totalMoney = computed(() => {
            return cart.value.reduce((sum, item) => {
                if (item.check) {
                    sum += item.price * item.count
                }
                return sum
            }, 0)

        })
        return {
            cart,
            totalAmount,
            totalMoney,
            cartItem,
            checkAll
        }
    },
    persist: {
        storage: sessionStorage
    }
})
export default useCartStore